package cn.iocoder.yudao.module.myerp.service.SKUproduct;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.myerp.controller.admin.SKUproduct.vo.*;
import cn.iocoder.yudao.module.myerp.dal.dataobject.SKUproduct.ErpProductSkuDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.myerp.dal.mysql.SKUproduct.ErpProductSkuMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.myerp.enums.ErrorCodeConstants.*;

/**
 * ERP SKU级产品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ErpProductSkuServiceImpl implements ErpProductSkuService {

    @Resource
    private ErpProductSkuMapper erpProductSkuMapper;

    @Override
    public Long createErpProductSku(ErpProductSkuSaveReqVO createReqVO) {
        // 插入
        ErpProductSkuDO erpProductSku = BeanUtils.toBean(createReqVO, ErpProductSkuDO.class);
        erpProductSkuMapper.insert(erpProductSku);
        // 返回
        return erpProductSku.getId();
    }

    @Override
    public void updateErpProductSku(ErpProductSkuSaveReqVO updateReqVO) {
        // 校验存在
        validateErpProductSkuExists(updateReqVO.getId());
        // 更新
        ErpProductSkuDO updateObj = BeanUtils.toBean(updateReqVO, ErpProductSkuDO.class);
        erpProductSkuMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpProductSku(Long id) {
        // 校验存在
        validateErpProductSkuExists(id);
        // 删除
        erpProductSkuMapper.deleteById(id);
    }

    private void validateErpProductSkuExists(Long id) {
        if (erpProductSkuMapper.selectById(id) == null) {
            throw exception(ERP_PRODUCT_SKU_NOT_EXISTS);
        }
    }

    @Override
    public ErpProductSkuDO getErpProductSku(Long id) {
        return erpProductSkuMapper.selectById(id);
    }

    @Override
    public PageResult<ErpProductSkuDO> getErpProductSkuPage(ErpProductSkuPageReqVO pageReqVO) {
        return erpProductSkuMapper.selectPage(pageReqVO);
    }

}